import java.io.*;
import java.util.*;

/*
 ID: ning yusui
 LANG: JAVA
 TASK: maze1
 */

public class maze1 {
    Scanner in;
    PrintWriter out;
    private char[][] grid;
    private int gridCol;
    private int gridRow;
    private int[] distance;


    public maze1() throws IOException {
        in = new Scanner(new File(this.getClass().getName() + ".in"));
        out = new PrintWriter(new BufferedWriter(new FileWriter(this.getClass().getName() +
                ".out")));
    }

    private void work() {
        Queue<Position> openQueue = input();
        int ans = searchBfs(openQueue);
        out.println(ans);
        out.flush();
        out.close();
    }

    int searchBfs(Queue<Position> openQueue) {
        int maxDistance = 0;
        while (!openQueue.isEmpty()) {
            Position pos = openQueue.poll();
            int posHashCode = pos.calcHashCode();
            for (int i = 0; i < pos.moveDist.length; i++) {
                Position nextPosition = new Position(pos.row + pos.moveDist[i][0], pos.col + pos.moveDist[i][1]);
                if (nextPosition.isValid()) {
                    int nextHashCode = nextPosition.calcHashCode();
                    if (distance[nextHashCode] == -1) {
                        distance[nextHashCode] = distance[posHashCode] + 1;
                        openQueue.add(nextPosition);
                        maxDistance = distance[nextHashCode];
                        grid[nextPosition.row][nextPosition.col] = (char) ('A' + maxDistance);
                    }
                }
            }
        }
//        for (int i = 0; i < grid.length; i++) {
//            System.out.println(Arrays.toString(grid[i]));
//        }
        return (maxDistance + 1) / 2;
    }

    private Queue<Position> input() {
        Queue<Position> exits = new ArrayDeque<Position>(6000) {
        };
        gridCol = in.nextInt() * 2 + 1;
        gridRow = in.nextInt() * 2 + 1;
        in.nextLine();
        distance = new int[gridRow * gridCol];
        Arrays.fill(distance, -1);
        grid = new char[gridRow][gridCol];
        for (int i = 0; i < grid.length; i++) {
            String line = in.nextLine();
            grid[i] = line.toCharArray();
            if (grid[i][0] == ' ') {
                addExits(exits, i, 0);
            } else if (grid[i][gridCol - 1] == ' ') {
                addExits(exits, i, gridCol - 1);
            }
            if (i == 0 || i == gridRow - 1) {
                int indexOf = line.indexOf(' ');
                while (indexOf > -1) {
                    addExits(exits, i, indexOf);
                    indexOf = line.indexOf(' ', indexOf + 1);
                }
            }
        }
        return exits;
    }

    private void addExits(Queue<Position> exits, int row, int col) {
        Position e = new Position(row, col);
        distance[e.calcHashCode()] = 0;
        exits.add(e);
    }

    public static void main(String[] args) throws IOException {
        new maze1().work();
    }

    class Position {
        int row;
        int col;

        protected final int moveDist[][] = {
                {-1, 0}, //N
                {0, 1},  //E
                {1, 0},  //S
                {0, -1}, //W
        };

        Position(int row, int col) {
            this.row = row;
            this.col = col;
        }

        public int calcHashCode() {
            int result = row * maze1.this.gridCol + col;
            return result;
        }

        public boolean isValid() {
            return col >= 0 && col < maze1.this.gridCol && row >= 0 && row < maze1.this.gridRow
                    && grid[row][col] == ' ';
        }
    }
}
